package Others;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;

import MainMethods.FileWr;
import MainMethods.HoverImage;
import MainMethods.Screenshot;

public class Search {
	HoverImage hover = new HoverImage();
	Screenshot scsh = new Screenshot();

	// check if search works
	public void searchWork(WebDriver driver, FileWr file, String screenDir) {
		try {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			driver.findElement(By.id("search")).clear();
			driver.findElement(By.id("search")).sendKeys("evil");
			driver.findElement(By.id("search_button")).click();
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			// add to collection from search result page
			addToCollection(driver, file, screenDir);
			// play movie from search result page
			playMovie(driver, file, screenDir);
			file.writeFile("search work - PASSED");
		} catch (NoSuchElementException e) {
			file.writeFile("search work - FAILED");
		}

	}

	// add to collection from search result page
	private void addToCollection(WebDriver driver, FileWr file, String screenDir) {
		try {

			driver.findElement(
					
					By.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[6]/a/div[1]/div/div[2]"));
			driver.findElement(
					By.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[6]/div/div[2]/button"))
					.click();
			driver.findElement(
					By.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[6]/div/div[2]/ul/li[2]/a"))
					.click();
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (hover.doesElementExist(driver, By.className("message")) == true) {
				file.writeFile("item added to collection - PASSED");
			} else {
				file.writeFile("item added to collection - FAILED");
			}
		} catch (NoSuchElementException e) {
			file.writeFile("added to collection - FAILED exception");
		}
	}

	private void playMovie(WebDriver driver, FileWr file, String screenDir) {
		try {
			driver.findElement(
					By.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[3]/div/div/a/span"))
					.click();
			driver.findElement(
					By.xpath("//*[@id=\"browse_wrapper\"]/div[2]/div[3]/div/div/div/ul/li[2]/a"))
					.click();
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			if (hover.doesElementExist(driver,
					By.xpath("//*[@id=\"video-player\"]")) == true) {
				file.writeFile("item can be played - PASSED");
			} else {
				file.writeFile("item can be played - FAILED");
			}
			driver.navigate().back();
		} catch (NoSuchElementException e) {
			file.writeFile("item can be played - FAILED exception");
		}
	}
}
